package com.cq.hd.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.BusinessInfo;
import com.cq.hd.common.enums.ResponseEnums;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.TbBusinessApi;
import com.cq.hd.member.api.TbBusinessMerchantApi;
import com.cq.hd.member.api.TbMerchantApi;
import com.cq.hd.member.api.vo.BusinessPageVo;
import com.cq.hd.member.api.vo.BusinessVo;
import com.cq.hd.member.api.vo.MerchantPageVo;
import com.cq.hd.system.api.TbHfMccApi;
import com.cq.hd.system.api.vo.HfMccVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Api(tags = "汇付所属行业相关接口")
@Slf4j
@RestController
@RequestMapping("/hfMcc")
public class HfMccController {

    @Autowired
    private TbHfMccApi mccApi;

    @Autowired
    private TbBusinessApi businessApi;

    @Autowired
    private TbMerchantApi merchantApi;

    @Autowired
    private TbBusinessMerchantApi businessMerchantApi;

    @ApiOperation(value = "根据id获取汇付的所属行业列表数据")
    @ApiImplicitParam(name = "id", value = "id:0：获取一级行业，其他id：获取对应下级数据", defaultValue = "0", paramType = "query", dataType = "Integer")
    @ApiResponse(response = HfMccVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/listById")
    public Result<List<HfMccVo>> listHfMccById(Integer id) {
        return mccApi.listHfMccById(id);
    }

    @ApiOperation(value = "获取与该商家关联的商户的行业类型")
    @ApiResponse(response = HfMccVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/listRelationMcc")
    public Result<List<HfMccVo>> listHfMccByGrade(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        Long businessId = 0L;
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            businessId = businessInfo.getId();
        }

        if (businessId == null || businessId <= 0) {
            Long userId = appLoginUser.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }

            // 根据userId查询商家id
            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo == null || businessPageVo.getId() == null || businessPageVo.getId() <= 0) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }
        }

        Set<String> mccList = new HashSet<>();
        // 获取与该商家关联的商户的行业类型
        List<MerchantPageVo> merchantPageVos = businessMerchantApi.listMerchantByBusinessId(businessId).unpack();
        if (!CollectionUtils.isEmpty(merchantPageVos)) {
            merchantPageVos = merchantPageVos.stream().filter(businessVo -> (!StringUtils.isBlank(businessVo.getMcc())))
                    .collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(merchantPageVos)) {
                mccList = merchantPageVos.stream().map(MerchantPageVo::getMcc).collect(Collectors.toSet());
            }
        }

        return mccApi.listHfMccByMccCodes(mccList);
    }

    @ApiOperation(value = "获取行业类型")
    @ApiResponse(response = HfMccVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/listMcc")
    public Result<List<HfMccVo>> listHfMccByGrade() {
        return mccApi.listHfMccByGrade(3);
    }


}
